<?php
declare(strict_types=1);
 
namespace App\JsonRpc\ServiceProvider;

use App\Constants\ErrorCode;
use App\Service\OilChannelService;
use App\Utils\ApiUtils;
use App\Validate\ChannelRpcValidation;
use Hyperf\RpcServer\Annotation\RpcService;
use App\Utils\ServerUtil;
use Hyperf\Utils\ApplicationContext;

/**
 * 注意，如希望通过服务中心来管理服务，需在注解内增加 publishTo 属性
 * @RpcService(name="ChannelService", protocol="jsonrpc", server="jsonrpc")
 */
class ChannelService implements ChannelServiceInterface
{
    public function consume($data): string{
        $data = json_decode($data,true);
        ServerUtil::info('rpc-ChannelService-进入', ['path'=>'ChannelService', 'parameter'=>$data]);
        $type = empty($data['type']) ? 1 : $data['type'];
        $obj = ApplicationContext::getContainer()->get(\App\Service\ChannelService::class);
        $channelValidation = ApplicationContext::getContainer()->get(ChannelRpcValidation::class);

        switch ($type)
        {
            case 1:
                //根据充值面值获取渠道信息
                $channelValidation->check($data, 'channel_amount')->response();;
                $res = $obj->amountByChannelInfo($data['recharge_type'], $data['operator_type'], $data['city_id'], $data['amount']);
                if(empty($res)){
                    $responseData = json_encode(ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用渠道'));
                }else{
                    $responseData = json_encode(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res));
                }
                break;
            case 2:
                //根据商户号获取渠道信息
                $channelValidation->check($data, 'channel_merchant_no')->response();
                $res = $obj->merchantNoByChannelInfo($data['merchant_no']);
                if($res){
                    $responseData = json_encode(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res));
                }else{
                    $responseData = json_encode(ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用渠道'));
                }
                break;
            case 3:
                //根据商户号获取合作商信息
                $channelValidation->check($data, 'channel_merchant_no')->response();
                $res = ApplicationContext::getContainer()->get(OilChannelService::class)->merchantNoByChannelInfo($data['merchant_no']);
                if($res){
                    $responseData = json_encode(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res));
                }else{
                    $responseData = json_encode(ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用合作商'));
                }
                break;
            case 4:
                //根据充值面值获取合作商信息
                $channelValidation->check($data, 'channel_amount_oli')->response();
                $res = ApplicationContext::getContainer()->get(OilChannelService::class)->amountByChannelInfo($data['ori_amount'], $data['amount'], $data['recharge_type']);
                if($res){
                    $responseData = json_encode(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$res));
                }else{
                    $responseData = json_encode(ApiUtils::send(ErrorCode::ERR_BUESSUS,'没有可用合作商'));
                }
                break;
            default:
                $responseData = json_encode(ApiUtils::send(ErrorCode::ERR_BUESSUS,'type解析失败'));
        }

        ServerUtil::info('rpc-ChannelService-退出', ['path'=>'ChannelService','param'=>$data,'res'=>$responseData]);
        return $responseData;
    }
}
